<template>
  <div class="allocation">
    <div class="common">
      <div class="common-button" @click="editHit(0)">新增</div>
    </div>

    <div class="table" v-loading="loading">
      <el-table :data="tableData" border :header-cell-style="{
        color: '#333333',
        backgroundColor: '#f3f3f3',
      }">
        <el-table-column prop="key" label="配置名称"> </el-table-column>
        <el-table-column prop="value" label="配置内容"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click.prevent="editHit(1, scope.row)">
                编辑
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
          layout="prev, pager, next" :total="tableDataTotal" :current-page="page">
        </el-pagination>
      </div>
    </div>

    <!--新增或者编辑-->
    <div class="add">
      <el-dialog :title="addRoaid == 0 ? '新增' : '编辑'" :visible.sync="addVisible" :before-close="addClose" width="30%">
        <div class="add-form">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="配置名称" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入配置名称" clearable></el-input>
            </el-form-item>

            <el-form-item label="配置内容" prop="text">
              <el-input v-model="ruleForm.text" placeholder="请输入配置内容" clearable></el-input>
            </el-form-item>

            <el-form-item class="attachment">
              <div class="list" @click="addClose('ruleForm')">取消</div>
              <div class="list" @click="addConfirm('ruleForm')">保存</div>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { commonsystemData, commonsystemDataAdd } from "@/api/common";

var throttle = require("@/utils/throttle"); // 防抖

export default {
  data() {
    return {
      //-------------
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      addRoaid: 0,
      allocationObj: {},
      loading: false,
      //-----------
      addVisible: false,
      ruleForm: {
        text: "",
        name: "",
      },
      rules: {
        name: [{ required: true, message: "请输入配置名称", trigger: "blur" }],
        text: [{ required: true, message: "请输入配置名称", trigger: "blur" }],
      },
    };
  },
  created() {
    this.loading = true;
    this.commonsystemDatatMet();
  },
  methods: {
    commonsystemDatatMet() {
      let params = {
        pageSize: this.limit,
        pageNo: this.page,
      };

      commonsystemData(params).then((res) => {
        if (res.errorCode == null) {
          this.tableData = res.resultData;
          this.tableDataTotal = res.resultData.length;

          this.loading = false;
        }
      });
    },
    //-------------
    editHit(index, item) {
      if (index == 1) {
        this.allocationObj = item;
        this.ruleForm.name = item.key;
        this.ruleForm.text = item.value;
      } else {
        this.allocationObj = {};
        this.ruleForm.name = "";
        this.ruleForm.text = "";
      }

      this.addRoaid = index;
      this.addVisible = true;
    },
    addClose() {
      this.allocationObj = {};
      this.ruleForm.name = "";
      this.ruleForm.text = "";
      this.addVisible = false;
      this.$refs[formName].resetFields();
    },
    addConfirm: throttle.throttle(function (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            systemId: this.allocationObj.systemId,
            key: this.ruleForm.name,
            value: this.ruleForm.text,
          };

          commonsystemDataAdd(params).then((res) => {
            if (res.errorCode == null) {
              if (this.addRoaid == 0) {
                this.$message.success("新增配置成功！");
              } else {
                this.$message.success("编辑配置成功！");
              }

              this.loading = true;
              this.commonsystemDatatMet();
              this.addVisible = false;
            }
          });
        } else {
          return false;
        }
      });
    }, 3000),
    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.commonsystemDatatMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.commonsystemDatatMet();
    },
  },
};
</script>

<style lang="less" scoped>
.allocation {
  .common {
    margin-bottom: 10px;
    display: flex;
    align-items: center;

    .common-button {
      padding: 9px 20px;
      box-sizing: border-box;
      border-radius: 5px;
      background: #2a82e4;
      font-size: 14px;
      color: #fff;
      cursor: pointer;
      margin-left: 0;
    }
  }

  .table {
    cursor: pointer;

    .operation {
      display: flex;
      align-items: center;
      color: #2a82e4;
    }
  }

  .add {
    /deep/ .el-dialog__header {
      font-size: 16px;
      text-align: center;
    }

    /deep/ .el-dialog__body {
      padding-bottom: 10px;
    }

    .add-form {
      /deep/ .el-select {
        width: 100%;
      }

      .img {
        width: 70px;
        height: 70px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 5px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .attachment {
        margin-top: 50px;

        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-form-item__content {
          margin-left: 0 !important;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .list {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 50px;
          border-radius: 5px;
          width: 100px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .list:first-child {
          background: #808080;
        }

        .list:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>